package com.augurit.common.calendar.convert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.augurit.common.base.extmodel.inner.OrgTreeNode;
import com.augurit.common.calendar.web.extmodel.OrgNode;
import com.augurit.common.calendar.web.extmodel.UserNode;
import com.augurit.common.calendar.web.form.SysCalSharedForm;
import com.augurit.common.calendar.web.form.SysCalendarForm;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.SysCalendar;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.system.web.org.form.OmUserForm;
import com.augurit.common.util.lang.DateUtils;

public class SysCalendarConvertor {
	/**
	 * 实体转换为form
	 * @param entity
	 * @return
	 */
	public static SysCalendarForm convertVoToForm(SysCalendar entity) {
		if(entity != null) {
			SysCalendarForm form = new SysCalendarForm();
			form.setEventId(entity.getEventId());
			form.setEventTypeId(entity.getEventTypeId());
			form.setTitle(entity.getTitle());
			form.setStartDate(entity.getStartDate());
			form.setEndDate(entity.getEndDate());
			form.setIsAllDay(entity.getIsAllDay());
			form.setIsNew(entity.getIsNew());
			form.setUrl(entity.getUrl());
			form.setLocation(entity.getLocation());
			form.setContent(entity.getContent());
			form.setReminder(entity.getReminder());
			
			form.setCreatorId(entity.getCreatorId());
			form.setOwnerId(entity.getOwnerId());
			form.setIsPublic(entity.getIsPublic());
			form.reSetStartTime();
			form.reSetEndTime();
			return form;
		}else
			return null;
	}
	
	/**
	 * form转换为实体
	 * @param form
	 * @param entity
	 */
	public static void convertFormToVo(SysCalendarForm form, SysCalendar entity) {
		if(entity != null && form != null) {
			entity.setEventId(form.getEventId());
			entity.setEventTypeId(form.getEventTypeId());
			if(form.getTitle() != null && form.getTitle().trim().length() > 0)
				entity.setTitle(form.getTitle().trim());
			entity.setStartDate(form.getStartDate());
			entity.setEndDate(form.getEndDate());
			if(form.getIsAllDay() != null && form.getIsAllDay().trim().length() > 0)
				entity.setIsAllDay(form.getIsAllDay().trim());
			if(form.getIsNew() != null && form.getIsNew().trim().length() > 0)
				entity.setIsNew(form.getIsNew().trim());
			if(form.getUrl() != null && form.getUrl().trim().length() > 0)
				entity.setUrl(form.getUrl().trim());
			if(form.getLocation() != null && form.getLocation().trim().length() > 0)
				entity.setLocation(form.getLocation().trim());
			if(form.getContent() != null && form.getContent().trim().length() > 0)
				entity.setContent(form.getContent().trim());
			entity.setReminder(form.getReminder());
			entity.setCreatorId(form.getCreatorId());
			entity.setOwnerId(form.getOwnerId());
			entity.setIsPublic(form.getIsPublic());
			
			form.reSetStartTime();
			form.reSetEndTime();
		}
	}
	
	public static List<SysCalendarForm> convertVoListToFormList(List<SysCalendar> caEventList) {
		if(caEventList != null && caEventList.size() > 0) {
			List<SysCalendarForm> caEventFormList = new ArrayList();
			for(int i=0; i<caEventList.size(); i++) {
				caEventFormList.add(convertVoToForm(caEventList.get(i)));
			}
			return caEventFormList;
		}
		return null;
	}
	
	public static List<SysCalendarForm> convertObjsToFormList(List<Object[]> objs){
		List<SysCalendarForm> result = null;
		if(objs != null && objs.size() > 0){
			result = new ArrayList<SysCalendarForm>();
			for(Object[] obj:objs){
				SysCalendar entity = (SysCalendar)obj[0];
				String ownerName = (String)obj[1];
				String shareType = (String)obj[2];
				String creatorName = (String)obj[3];
				
				SysCalendarForm form =  convertVoToForm(entity);
				form.setOwnerName(ownerName);
				form.setCreatorName(creatorName);
				form.setCanEdit(shareType.equals(SysCalSharedForm.WRITE_SHARE_TYPE)?true:false);
				result.add(form);
			}
			return result;
		}
		return null;
	}
	
	public static List<Map> convertVoListToMapList(List<SysCalendar> caEventList) {
		if(caEventList != null && caEventList.size() > 0) {
			List<Map> mapList = new ArrayList();
			for(int i=0; i<caEventList.size(); i++) {
				SysCalendar entity = caEventList.get(i);
				Map<String, String> map = new HashMap();

				map.put("eventId", entity.getEventId() == null ? "" : entity.getEventId().toString());
				map.put("calendarId", entity.getEventTypeId() == null ? "" : entity.getEventTypeId().toString());
				map.put("title", entity.getTitle());
				map.put("startDate", DateUtils.dateTimeToString(entity.getStartDate(), "yyyy-MM-dd"));
				map.put("endDate", DateUtils.dateTimeToString(entity.getEndDate(), "yyyy-MM-dd"));
				map.put("isAllDay", entity.getIsAllDay());
				map.put("isNew", entity.getIsNew());
				map.put("url", entity.getUrl());
				map.put("location", entity.getLocation());
				map.put("notes", entity.getContent());
				map.put("reminder", entity.getReminder() == null ? "" : entity.getReminder().toString());
				
				mapList.add(map);
			}
			return mapList;
		}
		return null;
	}
	
	public static List<SysCalendar> convertFormListToVoList(List<SysCalendarForm> caEventFormList) {
		if(caEventFormList != null && caEventFormList.size() > 0) {
			List<SysCalendar> caEventList = new ArrayList();
			for(int i=0; i<caEventFormList.size(); i++) {
				SysCalendar caEvent = new SysCalendar();
				convertFormToVo(caEventFormList.get(i), caEvent);
				caEventList.add(caEvent);
			}
			return caEventList;
		}
		return null;
	}
	
	public static OrgNode convertToExtNotLeafNode(OmOrgForm form){
		OrgNode node = null;
		if(form != null){
			node = new OrgNode();
			node.setKeyWord(form.getOrgId());
			node.setText(form.getOrgName());
			node.setIconCls(OrgTreeNode.CLS_ORG);
			
			if(form.getChildren() == null && form.getUsers() == null)
				node.setLeaf(true);
			else
				node.setLeaf(false);
		}
		return node;
	}
	
	public static UserNode convertToExtCheckNode(OmUserForm form){
		UserNode node = null;
		if(form != null){
			node = new UserNode();
			node.setKeyWord(form.getUserId());
			node.setText(form.getUserName());
			node.setLeaf(true);
			node.setIconCls(OrgTreeNode.CLS_USER_MALE);
			
			if(form.getSex() != null){
				if(form.getSex().equals(OrgConstant.SEX_MALE)){
					node.setIconCls(OrgTreeNode.CLS_USER_MALE);
				}
				else{
					node.setIconCls(OrgTreeNode.CLS_USER_FEMALE);
				}
			}
			node.setChecked(false);
		}
		return node;
	}
	
	public static List convertToExtCheckNodeList(List<OmUserForm> users){
		List result = null;
		if( users != null && users.size() > 0){
			result = new ArrayList<UserNode>();
			for(OmUserForm form:users){
				UserNode node = convertToExtCheckNode(form);
				result.add(node);
			}
		}
		return result;
	}
	
	
	/**
	 * 把带树型结构的机构form对象转换为一个树
	 * @param curNode
	 * @param form
	 * @return
	 */
	public static OrgNode convertToOrgTree(OrgNode curNode, OmOrgForm form){
		if(form != null){
			//根结点
			if( curNode == null)
				curNode = convertToExtNotLeafNode(form);
			
			List<OmUserForm> childUsers = form.getUsers();
			if(childUsers != null && childUsers.size() > 0){
				curNode.setChildren(convertToExtCheckNodeList(childUsers));
			}
			
			List<OmOrgForm> childOrgs = form.getChildren();
			if(childOrgs != null && childOrgs.size() > 0){
				for(OmOrgForm childOrg:childOrgs){
					OrgNode node = convertToExtNotLeafNode(childOrg);
					if(curNode.getChildren() == null)
						curNode.setChildren(new ArrayList());
					curNode.getChildren().add(node);
					convertToOrgTree(node, childOrg);
				}
			}
		}
		return curNode;
	}
	
	/**
	 * 把机构列表中的每个机构转换一棵树
	 * @param orgs
	 * @return
	 */
	public static List<OrgNode> convertToOrgTrees(List<OmOrgForm> orgs){
		List<OrgNode> result = null;
		if(orgs != null && orgs.size() > 0){
			result = new ArrayList<OrgNode>();
			for(OmOrgForm org:orgs){
				result.add(convertToOrgTree(null, org));
			}
		}
		return result;
	}
	
	
}